<?php
class Category extends CI_Model {
    public function __construct(){
        parent::__construct();
    }
    public function get_first_uid( $uid )	{
        $uid_ary = explode( ',', $uid );
        array_pop( $uid_ary );
        array_pop( $uid_ary );
        $uid     = implode( ',', $uid_ary ) . ',';
        return $uid;
    }
    public function get_cate_id($uid){
        $uid_ary = explode(',', $uid);
        $cate_id = $uid_ary[count($uid_ary)-2];
        return $cate_id;
    }
    public function output_category_ary($db_name='ca_news_category', $field='*', $where='uid="0,"', $ext_where=1){
        $CI =& get_instance();
        $cate_ary = array();
        $cate_row = $CI->data->get_all($db_name, $where, $field, 'my_order desc, cate_id asc');
        $this->output_category_ary_ext($cate_row, $db_name, $field, $ext_where, 0, '', $cate_ary);
        return $cate_ary;
    }

    public function output_category_ary_ext($cate_row, $db_name, $field, $ext_where, $layer, $pre_chars, &$cate_ary){	//递归循环输出类别到数组
        $CI =& get_instance();
        $row_count=count($cate_row);
        for($i=0; $i<$row_count; $i++){
            $cate_row[$i]['pre_chars']=str_replace(' ', '&nbsp;', $pre_chars.($i+1==$row_count?'└':'├'));
            $cate_ary[]=$cate_row[$i];
            if($cate_row_ext=$CI->data->get_all($db_name, "uid='{$cate_row[$i]['uid']}{$cate_row[$i]['cate_id']},' and $ext_where", $field, 'my_order desc, cate_id asc')){
                $pre_chars.=$i+1==$row_count?'  ':'｜';   //前导符
                $layer++;
                $this->output_category_ary_ext($cate_row_ext, $db_name, $field, $ext_where, $layer, $pre_chars, $cate_ary);
                $pre_chars=substr($pre_chars, 0, -3);
                $layer--;
            }
        }
    }
	
    public function get_uid($cate_id, $db_name='ca_product_category'){	//根据CateId取得UId
        $CI =& get_instance();
        $uid = $CI->data->get_value($db_name, "cate_id='$cate_id'", 'uid');
        return $uid?$uid.$cate_id.',':'-1,-1,';
    }

    public function output_category_select($select_name='cate_id', $selected_cate_id='', $db_name='ca_news_category', $where='uid="0,"', $ext_where=1, $select_value=''){//把类别表输出成一个select表单
        $CI =& get_instance();
        ob_start();
        echo "<select name='$select_name' class='form-control'>";
        $selected=$selected_cate_id==''?'selected':'';
        echo "<option value='' $selected>--$select_value--</option>";
        $cate_row=$CI->data->get_all($db_name, $where, 'uid,cate_id,category', 'my_order desc, cate_id asc');
        $this->output_category_select_ext($cate_row, $selected_cate_id, $db_name, 0, '', $ext_where);
        echo '</select>';
        $select=ob_get_contents();
        ob_end_clean();
        return $select;
    }

    public function output_category_select_ext($cate_row, $selected_cate_id, $db_name, $layer=0, $pre_chars='', $ext_where){
        $CI =& get_instance();
        $row_count=count($cate_row);
        for($i=0; $i<$row_count; $i++){
            $value=str_replace(' ', '&nbsp;', $pre_chars.($i+1==$row_count?'└':'├'));
            $selected=$cate_row[$i]['cate_id']==$selected_cate_id?'selected':'';
            echo "<option value='{$cate_row[$i]['cate_id']}' $selected>{$value}";
            echo $cate_row[$i]['category'];
            echo "</option>\n";
            if($cate_row_ext=$CI->data->get_all($db_name, "uid='{$cate_row[$i]['uid']}{$cate_row[$i]['cate_id']},' and $ext_where", 'uid,cate_id,category', 'my_order desc, cate_id asc')){
                $pre_chars.=$i+1==$row_count?'    ':'｜';   //前导符
                $layer++;
                $this->output_category_select_ext($cate_row_ext, $selected_cate_id, $db_name, $layer, $pre_chars, $ext_where);
                $pre_chars=substr($pre_chars, 0, -3);
                $layer--;
            }
        }
    }

    public function path($cate_id,$db_name='ca_news_category'){
        $CI =& get_instance();
        $category = $CI->data->get_one($db_name,"cate_id='$cate_id'");
        $all_cate = $CI->data->get_all($db_name,"1=1");
        $info     = array();
        foreach($all_cate as $k=>$v){
            $info[$v['cate_id']]=$v['category'];
        }
        $uid     = $category['uid'];
        $uid_ary = explode(',',$uid);
        $str     = '';
        foreach($uid_ary as $k=>$v){
            if($v){
                $str .= $info[$v].' > ';
            }
        }
        return $str.$CI->data->get_value($db_name,"cate_id='$cate_id'",'category');
    }
}